//
//  HELMaterialExamVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELMaterialExamVC.h"
#import "HELMaterialCell.h"
#import "HELPrepareForExamVC.h"
#import "HELSuperTableView.h"
#import "HELWebViewController.h"
#import "HELStatisticModel.h"

static NSString *cellID = @"HELMaterialCellID";
@interface HELMaterialExamVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
@property(nonatomic, assign)HELExamType type;
@property(nonatomic, copy)NSMutableArray *dataArray;
@end


@implementation HELMaterialExamVC


- (instancetype)initWithExamType:(HELExamType)type {
    if (self = [super init]) {
        _type = type;
        
    }
    return self;

}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor blueColor];
//    [self titleView:self.title];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
    [titleLabel setAdjustsFontSizeToFitWidth:YES];
    [titleLabel setText:self.title];
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.translucent = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"HELMaterialCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    weakSelf_define;
    // 添加下拉刷新头部控件
    [self.tableView wsj_HeaderRefreshBlock:^{
        //请求加载数据
        [weakSelf requestData:YES];
    }];
       [self requestData:YES];
    
    
    [self.tableView addLegendFooterWithRefreshingBlock:^{
          [weakSelf requestData:NO];
    }];
 

}

- (void)requestData:(BOOL)isRecover {
    

    self.tableView.loading = YES;
    NSString *url = nil;
    //NSDictionary *param = @{@"type":@(self.type), @"userId":userInfo.userId};
    static NSInteger currentPage = 1;
   
    
    if (isRecover) {
        currentPage =1;
    }else{
        currentPage ++;
    }
    
//    NSString *userId = [[HELAppManager userInfo] userId];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"pageSize"] = @(10);
//    params[@"currentPage"] =@(currentPage);
//    
//    switch (self.type) {
//        case HELExamTypeExamExcise_hospital:
//            if ([[HELAppManager userInfo] hospitalId]) {
//                params[@"hospitalId"]  =[[HELAppManager userInfo] hospitalId];
//            }else{
//                params[@"hospitalId"]  =@"";
//            }
//            url =helExamExciseUrl;
//            break;
//        case HELExamTypeExamExcise_public:
//            params[@"hospitalId"]  =@(-1);
//            url =helExamExciseUrl;
//            break;
//        case HELExamTypeMyExam_hospital:
//            url =helMyExamUrl;
//            params[@"userId"]  =userId;
//            break;
//        case HELExamTypeMyExam_record:
//            url =helStatisticUrl;  //
//            params[@"userId"]  =userId;
//            break;
//            
//        default:
//            break;
//    }
//    defineWeakSelf;
//    [[HELNetworkManager sharedMg] POST:url parameters:params success:^(id responseObject) {
//         [weakSelf.tableView.header endRefreshing];
//         [weakSelf.tableView.footer endRefreshing];
//         weakSelf.tableView.loading = NO;
//        if (weakSelf.type == HELExamTypeExamExcise_public || weakSelf.type == HELExamTypeExamExcise_hospital) {
//            NSInteger currentPage = [responseObject[@"page"] integerValue];
//            NSInteger total = [responseObject[@"pages"] integerValue];
//            if (total<=currentPage) {
//                [weakSelf.tableView.footer noticeNoMoreData];
//            }else{
//                [weakSelf.tableView.footer resetNoMoreData];
//            }
//            
//            if (isRecover) {
//                [weakSelf.dataArray removeAllObjects];
//            }
//            if ([responseObject isKindOfClass:[NSDictionary class]]) {
//                
//                if ([responseObject[@"rows"] isKindOfClass:[NSArray class]]) {
//                    
//                    [weakSelf.dataArray addObjectsFromArray:[HELExamModel mj_objectArrayWithKeyValuesArray:responseObject[@"rows"]]];
//                    
//                    [weakSelf.tableView reloadData];
//                }
//                
//            }
//            
//
//        }else if (weakSelf.type == HELExamTypeMyExam_hospital){
//            if ([responseObject isKindOfClass:[NSDictionary class]]) {
//                NSInteger currentPage = [responseObject[@"page"] integerValue];
//                NSInteger total = [responseObject[@"pages"] integerValue];
//                if (total<=currentPage) {
//                    [weakSelf.tableView.footer noticeNoMoreData];
//                }else{
//                    [weakSelf.tableView.footer resetNoMoreData];
//                }
//                if (isRecover) {
//                    [weakSelf.dataArray removeAllObjects];
//                }
//                 [weakSelf.dataArray addObjectsFromArray:[HELExamModel mj_objectArrayWithKeyValuesArray:responseObject[@"rows"]]];
//                 [weakSelf.tableView reloadData];
//            }
//        }else if (weakSelf.type == HELExamTypeMyExam_record){
//            NSInteger currentPage = [responseObject[@"page"] integerValue];
//            NSInteger total = [responseObject[@"pages"] integerValue];
//            if (total<=currentPage) {
//                [weakSelf.tableView.footer noticeNoMoreData];
//            }else{
//                [weakSelf.tableView.footer resetNoMoreData];
//            }
//            if (isRecover) {
//                [weakSelf.dataArray removeAllObjects];
//            }
//            [weakSelf.dataArray addObjectsFromArray:[HELStatisticModel mj_objectArrayWithKeyValuesArray:responseObject[@"rows"]]];
//            [weakSelf.tableView reloadData];
//            
//        }
//        
//    } failure:^(NSString *err) {
//        cwqHudRequestError(err);
//        [weakSelf.tableView.header endRefreshing];
//        [weakSelf.tableView.footer endRefreshing];
//        weakSelf.tableView.loading = NO;
//       
//        
//
//    }];
}


- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 40-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kAppBg;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView =[UIView new];
        _tableView.backgroundColor = kTableView_bg;
        _tableView.separatorEffect = UITableViewCellSeparatorStyleNone;

        [self.view addSubview:_tableView];
    }
    return  _tableView;
}


#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 128;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id model = self.dataArray[indexPath.row];
    HELMaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (self.type == HELExamTypeMyExam_record) {
        cell.record_model = model;
    }else{
        cell.model = model;
    }
    
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
  
//    if (self.type == HELExamTypeMyExam_record) {
//        HELStatisticModel *model = self.dataArray[indexPath.row];
//        HELWebViewController *webVC = [[HELWebViewController alloc] init];
//        NSString *url = [[[HELNetworkManager sharedMg] getBaseUrl] stringByAppendingString:[NSString stringWithFormat:@"%@?tblExamPaperId=%@&userId=%@",helExamDetailUrl ,model.tblExamPaperId,[HELAppManager userInfo].userId]];
//        webVC.url = [NSURL URLWithString:url];
//        webVC.title = model.title;
//        [self.navigationController pushViewController:webVC animated:YES];
//    }else{
//        HELExamModel *model = self.dataArray[indexPath.row];
//        HELPrepareForExamVC *prepareVC = [[HELPrepareForExamVC alloc] initWithExamId:model.id];
//        [self.navigationController pushViewController:prepareVC animated:YES];
//    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
